//
//  YBKPPTVC.m
//  FromDefinesTheModule
//
//  Created by  liyang on 15/6/14.
//  Copyright (c) 2015年 liyang. All rights reserved.
//

#import "YBKPPTVC.h"
#define YBKChangePPTTime 5.0f
#define YBKAnimationDurating .5f

@interface YBKPPTVC (){
    NSInteger count;//数组有多少个

}

@property(nonatomic,assign)NSInteger page;//页码

@property(nonatomic,strong)NSTimer *timer;//定时器

@end
static NSString * const identfier=@"pptVCcellIdentifier";
@implementation YBKPPTVC



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.pptShowView registerClass:[YBKPPTCell class] forCellWithReuseIdentifier:identfier];
    _pptShowView.pagingEnabled=YES;
    self.pptShowView.bounces=NO;
    
    UICollectionViewFlowLayout *collectionFloat= [[UICollectionViewFlowLayout alloc]init];
    collectionFloat.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    collectionFloat.minimumInteritemSpacing=0.0f;
    collectionFloat.minimumLineSpacing=0.0f;
    self.pptShowView.backgroundColor=[UIColor whiteColor];
    self.pptShowView.showsHorizontalScrollIndicator=NO;
    self.pptShowView.collectionViewLayout=collectionFloat;

}

-(void)setShowModelArray:(NSArray *)showModelArray{
    _showModelArray=showModelArray;
    count=showModelArray.count;
    self.pptPageControl.numberOfPages=count;
    self.pptPageControl.currentPage=0;
    [self timeOn];//开启定时
    [_pptShowView reloadData];

}



//开启一个定时器
-(void)timeOn{
    [self timeOff];
    if (count==0||self.pptShowView==nil||self.timer!=nil)  return;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:YBKChangePPTTime target:self selector:@selector(timerRunAction) userInfo:nil repeats:YES];
    
    //记录
    self.timer = timer;
}
//关闭定时器
-(void)timeOff{

    [self.timer invalidate];
    self.timer=nil;

}

#pragma mark 定时任务
-(void)timerRunAction{
    if (count-1==_page) {
        self.page=-1;
    }
    _page++;
   NSIndexPath *indexpath= [NSIndexPath indexPathForItem:_page inSection:0];
    [_pptShowView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
     [self.pptShowView.layer transitionWithAnimType:TransitionAnimTypeReveal subType:TransitionSubtypesFromRight curve:TransitionCurveEaseInEaseOut duration:YBKAnimationDurating];
    
    if (self.scrollBlock!=nil) {
        self.scrollBlock(_page);
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self timeOff];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self timeOn];
}


/*
 *  scrollView代理方法区
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSUInteger page = (NSUInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width + .5f);
    
    page = page % count;
    
    self.page = page;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.scrollBlock!=nil) {
        self.scrollBlock(_page);
    }
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    //关闭定时器
    [self timeOff];
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    //开启定时器
    [self timeOn];
}





#pragma mark --UICollectionViewdatasourceDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YBKPPTCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:identfier forIndexPath:indexPath];
    if (nil==cell) {
        cell=[[YBKPPTCell alloc]init];
        
    }
    [cell setModel:_showModelArray[indexPath.item]];
    return cell;
}



#pragma mark - UICollectionView delegate flowLayout下面这个代理主要是每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(YHGetSizeWidth(collectionView), YHGetSizeHeight(collectionView));
    
}
#pragma mark 这个代理方法是整个cellview距离左边的距离和右边的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.clickBlock!=nil) {
        self.clickBlock(_showModelArray[indexPath.item]);
    }
}





@end
